package com.guli.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.utils.R;
import com.guli.common.valid.AddGroup;
import com.guli.order.entity.OrderEntity;
import com.guli.order.service.OrderService;
import com.guli.order.vo.OrderAndOrderItemsVo;
import com.guli.order.vo.OrderSubmitVo;
import com.guli.order.vo.PreOrderInfoVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/*
 * @Author 罗俊
 * @date 2020/11/18 - 1:09 下午
 *
 */
@RestController
@Slf4j
@RequestMapping("/order/order")
public class OrderController {

    @Resource
    OrderService orderService;



    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取order分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size, @RequestParam Map<String, Object> params) {

        Page<OrderAndOrderItemsVo> page = orderService.queryPage(currentPage, size, params);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取Order")
    public R info(@PathVariable("id") Long id) {

        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    @PostMapping("/save")
    @ApiOperation("插入Order")
    public R save(@RequestBody OrderEntity order) {

        orderService.save(order);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新Order")
    public R update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除Order")
    public R delete(@RequestBody Long[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



    @ApiOperation("支付前确认订单")
    @GetMapping("/getPrePaymentInfo")
    public R getPrePaymentInfo() throws ExecutionException, InterruptedException {

        PreOrderInfoVo prePaymentInfo = orderService.getPrePaymentInfo();

        return R.ok().put("data",prePaymentInfo);
    }

    @ApiOperation("提交订单")
    @PostMapping("/submitOrder")
    public R submitOrder(@Validated(AddGroup.class) @RequestBody OrderSubmitVo orderSubmitVo) throws Throwable {

        String orderSn = orderService.submitOrder(orderSubmitVo);

        return R.ok().put("orderSn",orderSn);
    }

    @GetMapping("/getOrderByOrderSn/{orderSn}")
    @ApiOperation("根据OrderSn来获取订单")
    public R getOrderByOrderSn(@PathVariable("orderSn") String orderSn){

        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);

        return R.ok().put("data",orderEntity);
    }

}
